package com.zhz.common.tool.sms.boshitong;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.aliyun.dysmsapi20170525.models.*;
import com.zhz.common.tool.sms.SmsSender;
import com.zhz.common.tool.sms.properties.SmsProperties;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 博士通短信平台
 *
 * @author zhouhengzhe
 */
public class BoshitongSmsSender implements SmsSender {

    /**
     * 用户账号
     */
    private final String uid;
    /**
     * 用户密码
     */
    private final String pwd;
    /**
     * 发送长号
     */
    private final String srcPhone;

    private final String smsPkgUrl;

    private final String smsUrl;

    public static final String TEMPLATE_CODE = "验证码{+A+}，您正在为您的账号进行手机号码登录，感谢您的支持";

    public BoshitongSmsSender(SmsProperties.Boshitong boshitong) {
        this.uid = boshitong.getUid();
        this.pwd = boshitong.getPwd();
        this.srcPhone = boshitong.getSrcPhone();
        this.smsPkgUrl = boshitong.getSmsPkgUrl();
        this.smsUrl = boshitong.getSmsUrl();
    }

    private Map<String, Object> createBaseParams() {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("pwd", SecureUtil.md5(pwd));
        params.put("srcphone", srcPhone);
        return params;
    }

    @Override
    public boolean sendSingle(String phoneNumber, String template, Map<String, String> templateParams, String signName) {
        Map<String, Object> params = createBaseParams();
        params.put("mobile", phoneNumber);
        try {
            String content = template;
            for (String key : templateParams.keySet()) {
                content = content.replace(key, templateParams.get(key));
            }
            params.put("msg", URLEncoder.encode(signName + content, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String response = HttpUtil.post(smsUrl, params);
        String[] result = response.split(",");
        if ("0".equals(result[0])) {
            return true;
        }
        return false;
    }

    /**
     * 批量分组发送短信，比如多个1对1的短信发送
     *
     * @return
     */
    @Override
    public boolean sendBatch(String phoneNumberJson, String templateCode, String templateParams, String signNameJson) {
        return false;
    }

    /**
     * 查询短信发送统计信息
     *
     * @param querySendStatisticsRequest 查询条件
     * @return 查询结果
     */
    @Override
    public QuerySendStatisticsResponse querySendStatistics(QuerySendStatisticsRequest querySendStatisticsRequest) {
        return null;
    }

    /**
     * 查询短信发送详情
     *
     * @param querySendDetailsRequest 查询条件
     * @return 查询结果
     */
    @Override
    public QuerySendDetailsResponse querySendDetails(QuerySendDetailsRequest querySendDetailsRequest) {
        return null;
    }

    /**
     * 申请短信模板
     *
     * @param addSmsTemplateRequest 模板信息
     * @return 模板信息
     */
    @Override
    public AddSmsTemplateResponse addSmsTemplate(AddSmsTemplateRequest addSmsTemplateRequest) {
        return null;
    }

    /**
     * 查询短信模板列表
     *
     * @param querySmsTemplateListRequest 模板信息
     * @return 模板信息
     */
    @Override
    public QuerySmsTemplateListResponse querySmsTemplateList(QuerySmsTemplateListRequest querySmsTemplateListRequest) {
        return null;
    }
}
